const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
var loginStatus = wx.getStorageSync("loginStatus");
const {
  $Toast
} = require('../../components/base/base');
var interval = null;

Page({

  data: {
    navBg: app.globalData.navBg,
    navColor: app.globalData.navColor,
    windowHeight: app.globalData.windowHeight - app.globalData.navHeight,
    navTop: app.globalData.navHeight,
    back: true,
    navBox: true,
    home: true,
    email: '',
    smsCode: '',
    emailClose: false,
    smsCodeClose: false,
    smsTime: '获取验证码', //按钮文字
    currentTime: 60, //倒计时
    disabled: false, //按钮是否禁用
    detain: true
  },


  //按钮禁用
  detainButton: function () {
    let { email, smsCode } = this.data
    if (email && smsCode) {
      this.setData({
        detain: false
      })
    } else {
      this.setData({
        detain: true
      })
    }

  },

  //监听手机号
  emailInput: function (e) {
    var email = e.detail.value
    this.setData({
      email: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (email == null || email == undefined || email.length == 0 || email == '') {
      this.setData({
        emailClose: false
      })
    } else {
      this.setData({
        emailClose: true
      })
    }
    this.detainButton()
  },

  emailFocus: function (e) {
    var email = e.detail.value
    this.setData({
      email: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (email == null || email == undefined || email.length == 0 || email == '') {
      this.setData({
        emailClose: false
      })
    } else {
      this.setData({
        emailClose: true
      })
    }
  },

  emailBlur: function (e) {
    this.setData({
      emailClose: false
    })
  },

  CloseMInput: function (e) {
    var email = e.detail.value
    this.setData({
      email: '',
      emailClose: false
    });
    this.detainButton()
  },


  //监听短信验证码
  SmsCodeInput: function (e) {
    var smsCode = e.detail.value
    this.setData({
      smsCode: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (smsCode == null || smsCode == undefined || smsCode.length == 0 || smsCode == '') {
      this.setData({
        smsCodeClose: false
      })
    } else {
      this.setData({
        smsCodeClose: true
      })
    }
    this.detainButton()
  },

  SmsCodeFocus: function (e) {
    var smsCode = e.detail.value
    this.setData({
      smsCode: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (smsCode == null || smsCode == undefined || smsCode.length == 0 || smsCode == '') {
      this.setData({
        smsCodeClose: false
      })
    } else {
      this.setData({
        smsCodeClose: true
      })
    }
  },

  SmsCodeBlur: function (e) {
    this.setData({
      smsCodeClose: false
    })
  },

  CloseSCInput: function (e) {
    var smsCode = e.detail.value
    this.setData({
      smsCode: '',
      smsCodeClose: false
    });
    this.detainButton()
  },


  //修改邮箱
  exitEmail: util.throttle(function (e) {
    var that = this;
    var email = that.data.email;
    var smsCode = that.data.smsCode;
    if (email == "" || email == undefined || email == null || email.length == 0) {
      $Toast({
        content: '请输入邮箱!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(email)) {
      $Toast({
        content: '邮箱格式不正确!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (smsCode == "" || smsCode == undefined || smsCode == null || smsCode.length == 0) {
      $Toast({
        content: '请输入验证码!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (smsCode.length < 4 || smsCode.length > 6) {
      $Toast({
        content: '验证码错误!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else {

      $Toast({
        content: '修改成功!',
        icon: 'success',
        duration: 1
      });

      wx.navigateBack({
        delta: 1
      })
    }

  }, 1000),

  //验证码倒计时函数
  getSmsCode: function (options) {
    var that = this;
    that.setData({
      disabled: true, //只要点击了按钮就让按钮禁用 (避免正常情况下多次触发定时器事件)
      color: '#DEDEDE',
    })

    var email = that.data.email;
    var currentTime = that.data.currentTime
    var warn = null;
    if (email == "" || email == undefined || email == null || email.length == 0) {
      warn = "请输入邮箱";
    } else if (!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(email)) {
      warn = "邮箱格式不正确";
    } else {
      wx.request({
        // url: util.api + api.SendEmailCode,
        url: 'http://userapi.zhikaisoft.com/Customer/SendEmailCode',
        //url: 'http://userapi.zhikaisoft.com/Customer/SendSMSCode',
        
        data: {
          BUSINESS_PARAMETERS: {
            msgType: 1,
            email: '2807019098@qq.com'
            // mobile: "13816093632"
          },
          SYSTEM_PARAMETERS: {
            APP_ID: "M_P_G001",
            METHOD_ID: 0,
            ACCESS_TOKEN: "1w2Re3bE^og38",
            TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
            SIGN: "string"
          }
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          //当手机号正确的时候提示用户短信验证码已经发送
          $Toast({
            content: "短信验证码已发送",
            icon: 'prompt_fill',
            duration: 1
          })
          console.log(res.data.RESULT)  
          //设置一分钟的倒计时

          var interval = setInterval(function () {
            currentTime--; //每执行一次让倒计时秒数减一
            that.setData({
              smsTime: currentTime + 1 + 's', //按钮文字变成倒计时对应秒数
            })
            //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字
            if (currentTime <= 0) {
              clearInterval(interval)
              that.setData({
                smsTime: '重新发送',
                currentTime: 60,
                disabled: false,
                color: '#DEDEDE'
              })
            }

          }, 1000);
        }
      })
/*
      $Toast({
        content: "验证码已发送",
        icon: 'success',
        duration: 1
      })

      //设置一分钟的倒计时

      var interval = setInterval(function () {
        currentTime--; //每执行一次让倒计时秒数减一
        that.setData({
          smsTime: currentTime + 1 + 's', //按钮文字变成倒计时对应秒数
        })

        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            smsTime: '重新发送',
            currentTime: 60,
            disabled: false,
            color: '#DEDEDE'
          })
        }

      }, 1000);*/
    };

    if (warn != null) {
      $Toast({
        content: warn,
        icon: 'prompt_fill',
        duration: 1
      })
      that.setData({
        disabled: false,
        color: '#DEDEDE'
      })
      return;
    };

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

})